//
//  CinemaViewController.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaTableViewCell.h"
#import "CinemaModel.h"


@interface CinemaViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *arrAllData;
}
@end

@implementation CinemaViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //遵守协议
    self.TableView.delegate=self;
    self.TableView.dataSource=self;
    [self getDatas];
}
-(void)getDatas
{
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:CINEMAURL]];
    NSURLSession *session=[NSURLSession sharedSession];
    NSURLSessionTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data)
        {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            arrAllData=[NSMutableArray array];
      
            for (NSDictionary *dic in dict[@"result"][@"data"])
            {
                CinemaModel *model=[[CinemaModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
            [arrAllData addObject:model];
            }
        }
        [self performSelectorOnMainThread:@selector(mainAction)   withObject:nil waitUntilDone:nil];
    }];
    [task resume];
}
-(void)mainAction
{
    [self.HUD hide:YES];
    [self.TableView reloadData];
}
//代理要实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrAllData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cinema_id" forIndexPath:indexPath];
    CinemaModel *model=arrAllData[indexPath.row];
    cell.CinemaName.text=model.CinemaName;
    cell.Address.text=model.Address;
    cell.Telephone.text=model.Telephone;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
